﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.Logic.POI;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;
using Action = TreeSharp.Action;
using Sequence = TreeSharp.Sequence;
using Styx.WoWInternals;


namespace Healz.Druid.Spells
{
    public static class WildGrowth
    {
        public const string SpellName = "Wild Growth";

        public static bool Pulse()
        {
            // Can Cast / off coooldown
            if (!SpellManager.Spells[SpellName].CanCast) return false;
            if (SpellManager.Spells[SpellName].Cooldown) return false;

            WoWPlayer Best = ToWild();

            if (Best == StyxWoW.Me) return false;

            // Cast and write
            Utils.WriteCast(SpellName, Best);
            SpellManager.Cast(SpellName, Best);

            return true;
        }

        public static WoWPlayer ToWild()
        {
            // Calling local cause there local i guess.
            List<WoWPlayer> lLocalPlayer = PlayersAroundMe;
            WoWPlayer LocalBest = StyxWoW.Me;
            int LocalHighestHealing = 0;


            foreach (WoWPlayer LocalPlayer in lLocalPlayer)
            {
                int CurrentHealingDone = 0;
                int CurrentCount = 0;

                foreach (WoWPlayer CurPlayer in ObjectManager.GetObjectsOfType<WoWPlayer>(true,true))
                {
                    if (!CurPlayer.IsFriendly) continue;
                    if (CurPlayer.CurrentHealth <= 1) continue;
                    if (Distance(LocalPlayer, CurPlayer) > 30) continue;
                    if (CurPlayer.HealthPercent > 95) continue;

                    CurrentCount++;

                    float MissingHealth = (CurPlayer.MaxHealth - CurPlayer.CurrentHealth);
                    if (MissingHealth > 5000) MissingHealth = 5000;

                    CurrentHealingDone += (int)MissingHealth;
                }

                // We got our new best person. Put him on
                if (CurrentHealingDone > LocalHighestHealing)
                {
                    if (CurrentCount >= 2)
                    {
                        LocalHighestHealing = CurrentHealingDone;
                        LocalBest = LocalPlayer;
                    }
                }
            }

            if (LocalHighestHealing > 10000) Utils.WriteDebug("Wild Growth - Heal: {0}",  LocalHighestHealing);
            return LocalBest;
        }

        public static double Distance(WoWPlayer DistUnit1, WoWPlayer DistUnit2)
        {
            return Math.Sqrt(
                Math.Pow(DistUnit2.X - DistUnit1.X, 2) +
                Math.Pow(DistUnit2.Y - DistUnit1.Y, 2));
        }

        /// <summary>
        /// Get list of players around me
        /// </summary>
        public static List<WoWPlayer> PlayersAroundMe
        {
            get
            {
                return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true)
                        where Unit.IsFriendly
                        where Unit.Distance2D < 40
                        where Unit.CurrentHealth > 1
                        where !Unit.Dead
                        select Unit).ToList();
            }
        }
    }
}
